-- web config 
WIFI = 4
gpio.mode(WIFI, gpio.OUTPUT)
gpio.write(WIFI, gpio.LOW)
wifi.setmode(wifi.STATION)

function webconfig()
    print('\t start webconfig now, find a wifi called SetupGadget_xxx ... ')
    enduser_setup.start(
      function()
    --    print("Connected to wifi as:" .. wifi.sta.getip())
        print("turn off the light on board..")
        gpio.write(WIFI, gpio.HIGH)
        do
        local sta_config=wifi.sta.getconfig(true)
        if(sta_config)
        then
            print(string.format("\tCurrent station config\n\tssid:\"%s\"\tpassword:\"%s\"\n\tbssid:\"%s\"\tbssid_set:%s", sta_config.ssid, sta_config.pwd, sta_config.bssid, (sta_config.bssid_set and "true" or "false")))
            -- connect to Access Point (DO save config to flash)
            station_cfg={}
            station_cfg.ssid=sta_config.ssid
            station_cfg.pwd=sta_config.pwd
            station_cfg.save=true
            wifi.sta.config(station_cfg)
        end
        end
        dofile("kaiguan.lua")
        end,
      function(err, str)
        print("enduser_setup: Err #" .. err .. ": " .. str)
      end
    )
end

local sta_config=wifi.sta.getconfig(true)
print("\t store_config: "..sta_config.ssid)
if(sta_config.ssid ~= "")
then
    print("\tthis is wifi...")
    print(string.format("\tCurrent wifi station config\n\tssid:\"%s\"\tpassword:\"%s\"", sta_config.ssid, sta_config.pwd))
    station_cfg={}
    station_cfg.ssid=sta_config.ssid
    station_cfg.pwd=sta_config.pwd
--    station_cfg.ssid='mo201'
--    station_cfg.pwd='88888888'
--    station_cfg.auto=true
    station_cfg.save=true
    wifi.sta.config(station_cfg)
    wifi.sta.disconnect()
    gpio.write(WIFI, gpio.LOW)
    wifi.sta.connect()
    tmr.create():alarm(10000, tmr.ALARM_SINGLE, function()
        print(wifi.sta.getip())
   
    if(wifi.sta.getip() ~= nil)
    then
        print("turn off the light on board..")
        gpio.write(WIFI, gpio.HIGH)
        dofile("kaiguan.lua")
    else
        print('\t wifi connect fail')
        webconfig()
    end
    end)
else
    print('\t no wifi ')
    webconfig()
end




